package com.dreamthen.config;

import com.dreamthen.interceptor.CheckLoginInterceptor;
import com.dreamthen.interceptor.MyInterceptor;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

/**
 * 相当于spirngMvc相关配置
 * @Author zengms
 * @Date 2023-11-14 21:08:28
 **/
@Configuration
@ComponentScan(basePackages = "com.dreamthen")
@EnableWebMvc //相当于<mvc:annotation-driven/>
public class WebConfig implements WebMvcConfigurer {

    //相当于<mvc:default-servlet-handler>配置
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    //拦截器配置<mvc:interceptors>
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor())
                .addPathPatterns("/**"); // 拦截所有路径
        registry.addInterceptor(new CheckLoginInterceptor())
                .addPathPatterns("/**").excludePathPatterns("/user/login"); // 拦截所有路径
    }

    //视图控制器view-controller
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/admin").setViewName("admin");
    }

    //视图解析器InternalResourceViewResolver
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.jsp("/WEB-INF/views/", ".jsp");
    }
}
